﻿Imports System.Data.OleDb
Public Class HoaDonDAO
    Public Function GetHoaDon() As DataTable
        Dim sql As String = "select * from HoaDon"
        Dim dt As New DataTable
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim da As New OleDbDataAdapter(sql, cn)
        da.Fill(dt)
        cn.Close()
        Return dt
    End Function

    Public Sub AddHoaDon(ByVal newHD As HoaDonDTO)
        Dim sql As String = String.Format("insert into HoaDon(MaHD,NgayXuat,MaBan,TenMonAn,MaKH,MaNV,TinhTrang,TongTien) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", _
                                           newHD.MaHD, newHD.NgayXuat, newHD.MaBan, newHD.MonAnThanhToan, newHD.MaKH, newHD.MaNV, newHD.TinhTrang, newHD.TongTien)

        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Dữ liêu bị sai vui lòng kiểm tra lại !")
        End Try
        cn.Close()
    End Sub

    Public Sub DelHoaDon(ByVal delHD As HoaDonDTO)
        Dim sql As String = String.Format("delete from HoaDon where MaHD={0}", delHD.MaHD)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub

    Public Sub UpdateHoaDon(ByVal newHD As HoaDonDTO)
        Dim sql As String = String.Format("update HoaDon set NgayXuat='{0}',MaBan='{1}',TenMonAn='{2}',MaKH='{3}',MaNV='{4}',TinhTrang='{5}',TongTien='{6}' where MaHD={7}", _
                                          newHD.NgayXuat, newHD.MaBan, newHD.MonAnThanhToan, newHD.MaKH, newHD.MaNV, newHD.TinhTrang, newHD.TongTien, newHD.MaHD)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Vui lòng kiểu tra lại dữ liệu !")
        End Try
        cn.Close()
    End Sub
End Class
